﻿Imports BUS
Imports DAO
Imports DTO
Imports DevComponents.DotNetBar
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.IO
Imports System.Diagnostics

Public Class frmQuanLyTheLoaiSach
    Private tlHienTai As New TheLoaiDTO()


    Private Sub frmQuanLyTheLoaiSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        HienThiDanhSach()
    End Sub

    Public Sub HienThiDanhSach()
        dgvDanhSachTheLoai.DataSource = TheLoaiDAO.GetTheLoaiAll()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim tl As New TheLoaiDTO()
        tl.TenTheLoai = txtTenLoaiSach.Text
        If TheLoaiBUS.ThemTheLoai(tl) = False Then
            MessageBoxEx.Show("Dữ liệu nhập vào đã có trong cơ sở dũ liệu", "Thông báo")
        End If
        HienThiDanhSach()
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim tl As New TheLoaiDTO()
        tl.TenTheLoai = txtTenLoaiSach.Text
        If TheLoaiBUS.XoaTheLoai(tl) = False Then
            MessageBoxEx.Show("Xóa thất bại", "Thông báo")
        Else
            MessageBoxEx.Show("Xóa thành công", "Thông báo")
            HienThiDanhSach()
        End If
    End Sub

    Private Sub dgvDanhSachTheLoai_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDanhSachTheLoai.SelectionChanged
        If dgvDanhSachTheLoai.SelectedRows.Count > 0 Then
            txtMaLoaiSach.Text = dgvDanhSachTheLoai.SelectedRows(0).Cells(0).Value.ToString()
            txtTenLoaiSach.Text = dgvDanhSachTheLoai.SelectedRows(0).Cells(1).Value.ToString()
            'txtTenLoaiSach.Text = dgvDanhSachTheLoai.SelectedRows(0).Cells(1).Value.ToString()
            'Dim row As Integer = dgvDanhSachTheLoai.SelectedCells(0).RowIndex
            'DongToiTheLoaiHienTai(dgvDanhSachTheLoai.Rows(row))
            'txtMaLoaiSach.Text = tlHienTai.MaTheLoai.ToString()
            'txtMaLoaiSach.Text = tlHienTai.TenTheLoai
        End If
    End Sub

    'Private Sub DongToiTheLoaiHienTai(ByVal row As DataGridViewRow)
    '    tlHienTai.MaTheLoai = CInt(Fix(row.Cells(0).Value))
    '    tlHienTai.TenTheLoai = CStr(row.Cells(1).Value)
    'End Sub


    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim tl As New TheLoaiDTO()
        tl.TenTheLoai = txtTenLoaiSach.Text
        tl.MaTheLoai = Integer.Parse(txtMaLoaiSach.Text)
        If TheLoaiBUS.SuaTheLoai(tl) = False Then
            MessageBoxEx.Show("Cập nhật thất bại", "Thông báo")
        Else
            MessageBoxEx.Show("Cập nhật thành công", "Thông báo")
            HienThiDanhSach()
        End If
    End Sub

    Private Sub btnXuatFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXuatFile.Click
        Dim oldPath As String = Directory.GetCurrentDirectory()
        Dim sf As New SaveFileDialog()
        sf.ShowDialog()
        Dim strFileName As String = sf.FileName
        Directory.SetCurrentDirectory(oldPath)
        Dim myExcel As New ExcelWrite()
        Dim list As List(Of String) = myExcel.DGVToExcel(dgvDanhSachTheLoai)
        Dim filePath As String = Directory.GetCurrentDirectory() & strFileName
        myExcel.WriteDateToExcel(strFileName, list, "A1", "B1", "A2")
        MessageBoxEx.Show("Xuất danh sách khách hàng ra file Excel thành công", "Thông báo")
    End Sub

    Private Sub btnNhapFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapFile.Click
        Dim oldPath As String = Directory.GetCurrentDirectory()
        Dim [of] As New OpenFileDialog()
        [of].ShowDialog()
        Dim strPathName As String = [of].FileName
        Directory.SetCurrentDirectory(oldPath)
        Dim dt As DataTable = ExcelRead.getSheet(strPathName, "sheet1")
        For i As Integer = 0 To dt.Rows.Count - 1
            Dim s As New SachDTO()
            s.TenSach = dt.Rows(i).ItemArray(1).ToString()
            s.GiaBan = Integer.Parse(dt.Rows(i).ItemArray(2).ToString())
            s.SoLuongTon = Integer.Parse(dt.Rows(i).ItemArray(3).ToString())
            s.TacGia = dt.Rows(i).ItemArray(4).ToString()
            s.MaTheLoai = Integer.Parse(dt.Rows(i).ItemArray(5).ToString())
            SachDAO.Insert(s)
        Next i
        MessageBoxEx.Show("Nhập danh sách khách hàng từ Excel thành công", "Thông báo")
        HienThiDanhSach()
    End Sub
End Class